﻿// -
// <copyright file="ServiceUri.cs" company="Microsoft Corporation">
//    Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// -

namespace Hawaii.Services.Client
{
    using System;
    using System.Net;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// Helper class to create a Service Uri during a service invocation.
    /// </summary>
    public class ServiceUri
    {
        /// <summary>
        /// A Service host name
        /// 
        /// An example host name
        /// Service Url : http://hawaiispeech/SpeechRecognition
        /// Host Name   : hawaiisppech
        /// </summary>
        public string HostName { get; set; }

        /// <summary>
        /// A Service's REST signature
        /// 
        /// REST signature examples
        /// Service Url : http://hawaiispeech/SpeechRecognition
        /// Signature    : SpeechRecognition
        /// 
        /// Service Url : http://hawaiispeech/SpeechRecognition/GrammerName/
        /// Signature    : SpeechRecognition/GrammerName
        /// </summary>
        public string Signature { get; set; }

        /// <summary>
        /// Arguments are used for the Query String part of the Service Url.
        /// </summary>
        private List<KeyValuePair<string, string>> Arguments { get; set; }

        /// <summary>
        /// Default construtor
        /// </summary>
        public ServiceUri()
        {
            this.Arguments = new List<KeyValuePair<string, string>>();
        }

        /// <summary>
        /// Overloaded constructor
        /// </summary>
        /// <param name="hostName">Specifies the host name.</param>
        /// <param name="signature">Specifies the service signature.</param>
        public ServiceUri(string hostName, string signature)
        {
            this.HostName = hostName;
            this.Signature = signature;
            this.Arguments = new List<KeyValuePair<string, string>>();
        }

        /// <summary>
        /// A Helper method to add a query string key value pair.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddQueryString(string key, string value)
        {
            this.Arguments.Add(new KeyValuePair<string, string>(key, value));
        }

        /// <summary>
        /// The method returns the service uri string.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder address = new StringBuilder();
            // -
            // Create the main uri
            // -
            address.AppendFormat("http://{0}/{1}?", this.HostName, this.Signature);

            // -
            // Form the query string from the arguments collection.
            // -
            foreach (KeyValuePair<string, string> item in this.Arguments)
            {
                address.AppendFormat("{0}={1}", item.Key, item.Value);
                address.Append("&");
            }
            // -
            // Remove the last ambersand.
            // -
            address.Remove(address.Length - 1, 1);

            // -
            // Return the service uri string.
            // -
            return address.ToString();
        }
    }
}
